package com.tudouji.project.flow.utils;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.flow.FlowTempNode;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.pojo.user.FlowUser;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.flow.service.process.service.impl.FlowTempNodeServiceImpl;
import com.flow.service.utils.FlowProcessUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaxl
 * @version v1.0
 * @ClassName FlowProcessUtil
 * @Descrption
 * @date 2022/3/30 15:50
 */
@Component
public class FlowProcessUtil extends FlowProcessUtils {

//    @Autowired
//    @Qualifier("pendingAdviceService")
//    private PendingAdviceServiceImpl pendingAdviceService;
//
//    @Autowired
//    @Qualifier("ccAdviceService")
//    private CCAdviceServiceImpl ccAdviceService;
    @Autowired
    private FlowTempNodeServiceImpl nodeService;

    @Autowired
    private FlowProcessProxy flowProcessProxy;

    /**
     * @Description: 发起
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    @Transactional
    public FlowResult defaultStart(Long nodeId, BasePojo base, String fileIds, FlowUser user) {
        //return super.defaultStart(nodeId, base, fileIds, user);
        //20230313 调用新流程逻辑
        return flowProcessProxy.start(nodeId,base,fileIds,user);
    }

    /**
     * @Description: 退回
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    public FlowResult defaultBackApproval(Long nodeId,String content,BasePojo base, String fileIds,FlowUser user){
        //return super.defaultBackApproval(nodeId,base,content,fileIds,user);
        //20230313 调用新流程逻辑
        return flowProcessProxy.toSpecifiedNode(nodeId,base,content,fileIds,user);
    }

    /**
     * @Description: 通用审批
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    @Transactional
    public FlowResult defaultApproval(Long taskId, String fileIds, String opinion, BasePojo base, FlowUser user) {
        //return super.defaultApproval(taskId, fileIds, opinion, base,user);
        //20230313 调用新流程逻辑
        return flowProcessProxy.approval(taskId,fileIds,opinion,base,user);
    }

    /**
     * @Description: 拒绝
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    @Transactional
    public FlowResult defaultRefuse(Long taskId, String fileIds, String opinion, BasePojo base, FlowUser user,
                                    Long backNodeId, Long backUserId) {
        //return super.defaultRefuse(taskId, fileIds, opinion, base, user, backNodeId, backUserId);
        //20230313 调用新流程逻辑
        return flowProcessProxy.refuse(taskId, fileIds, opinion, base, user, backNodeId, backUserId);
    }

    /**
     * @Description: 流程记录
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    public List<Object> getApprovalRecord(Long orderId, Long flowId,BasePojo base) {
        return super.getApprovalRecord(orderId, flowId,base);
    }

    /**
     * @Description: 获取拒绝节点
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    public List<FlowTempNode> getRefuseNodes(Long orderId, Long taskId) {
        return super.getRefuseNodes(orderId, taskId);
    }


    /**
     * @Description:
     * @Author: xiaxl
     * @Date:
     * @Param: 转交
     * @return:
     **/
    @Transactional
    public FlowResult defaultTransmit(Long taskId, FlowUser fromUser, Long toUserId, String opinion, String fileIds) {
        //return super.defaultTransmit(taskId, fromUser, toUserId, opinion, fileIds);
        //20230313 调用新流程逻辑
        return flowProcessProxy.transmit(taskId, fromUser, toUserId, opinion, fileIds);
    }

    /**
     * @Description: 删除业务的时候 更新task 调用
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    @Transactional
    public void updateTaskByOrderId(Long orderid, FlowUser user, Long flowId) {
        super.updateTaskByOrderId(orderid, user, flowId);
    }


    /**
     * @Description: 分享
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    public FlowResult defaultShare(Long id, Long flowId, String userIds, FlowUser user,BasePojo base) {
        //return super.defaultShare(id, flowId, userIds, user,base);
        //20230313 调用新流程逻辑
        return flowProcessProxy.share(flowId,userIds,user,base);
    }

    @Transactional
    public FlowResult reply(String fileIds, Long operationId, Long opltId, String content, FlowUser user,BasePojo base) {
        return super.reply(fileIds, operationId, opltId, content, user, null,base);
    }

    @Transactional
    public FlowResult reply(Long orderId, Long flowId, String fileIds, String content, FlowUser user, BasePojo base){
        return super.reply(orderId,flowId,fileIds,null,null,content,user,null,base);
    }
    @Transactional
    public FlowResult reply2(Long flowId, String fileIds, String content, FlowUser user, BasePojo base){
//        return super.reply(orderId,flowId,fileIds,null,null,content,user,null,base);
        //20230313 调用新流程逻辑
        return flowProcessProxy.comment(flowId,fileIds,content,user,base);
    }
    @Override
    @Transactional
    public FlowResult cancelComment(Long orderId, FlowUser user){
        return super.cancelComment(orderId,user);
    }


    /**
     * @Description: 获取已操作过节点  （评论用）
     * @Author: xiaxl
     * @Date:
     * @Param:
     * @return:
     **/
    public List<FlowTempNode> getHandleNodes(Long orderId, Long flowId) {
        return super.getHandleNodes(orderId, flowId);
    }

    @Transactional
    public FlowResult cancelReply(Long id, FlowUser user) {
        return super.cancelReply(id, user);
    }

    public Long getStartNode(Long id) {
        // 获取起始节点
        FlowTempNode node = new FlowTempNode();
        node.setNodeType(1);
        node.setFlowtempId(id);
        FlowTempNode flowTempNode = nodeService.selectFlowTempNodeList(node).get(0);
        if (null != flowTempNode){
            return flowTempNode.getId();
        }
        return null;
    }

    public Long getStartNodeByType(Long id, Integer type) {
        // 获取起始节点
        FlowTempNode node = new FlowTempNode();
        node.setNodeType(type);
        node.setFlowtempId(id);
        FlowTempNode flowTempNode = nodeService.selectFlowTempNodeList(node).get(0);
        if (null != flowTempNode){
            return flowTempNode.getId();
        }
        return null;
    }

    public boolean getStartNodeByType(Long id, Integer type, Integer status) {
        // 获取起始节点
        FlowTempNode node = new FlowTempNode();
        node.setNodeType(type);
        node.setFlowtempId(id);
        FlowTempNode flowTempNode = nodeService.selectFlowTempNodeList(node).get(0);
        if (null != flowTempNode){
            return flowTempNode.getFlowStat().equals(status);
        }
        return false;
    }
}
